//-----------------------------------------------------------------------
// <copyright file="DefaultHandler.cs" company="iron9light">
//      Copyright (c) iron9light. All rights reserved.
// </copyright>
// <author>iron9light</author>
//-----------------------------------------------------------------------

namespace EventBroker.Handlers
{
    using System;
    using System.Reflection;

    /// <summary>
    /// Handler that executes the subscription on the same thread the publisher is currently running (synchronous).
    /// </summary>
    public class DefaultHandler : IHandler
    {
        private Delegate subscriptionHandler;

        public virtual Delegate SubscriptionHandler
        {
            get
            {
                return this.subscriptionHandler;
            }

            set
            {
                this.subscriptionHandler = value;
            }
        }

        public virtual void Handle(object sender, EventArgs e, System.Collections.Generic.ICollection<Exception> exceptions)
        {
            try
            {
                if (this.SubscriptionHandler != null)
                {
                    this.SubscriptionHandler.DynamicInvoke(sender, e);
                }
            }
            catch (TargetInvocationException ex)
            {
                exceptions.Add(ex.InnerException);
            }
        }
    }
}